/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dnbW.dbW;

import javax.swing.tree.DefaultMutableTreeNode;

/**
 * This is a double linked-list style path expression. 
 * @author zhouyong
 */
public class PathExpression2 {
    
    private Step rootStep;
    private Step lastStep;
    private String dataSource;
    private DatabaseInfo contextDb;

    public PathExpression2(Step rootStep, String dataSource, DatabaseInfo contextDb) {
        this.rootStep = rootStep;
        this.dataSource = dataSource;
        this.contextDb = contextDb;
        this.lastStep = constructLastStep();
    }

    public PathExpression2(Step rootStep, Step lastStep, String dataSource, DatabaseInfo contextDb) {
        this.rootStep = rootStep;
        this.lastStep = lastStep;
        this.dataSource = dataSource;
        this.contextDb = contextDb;
    }
    
    // adapter constructor
    public PathExpression2(PathExpression path) {
        Step root = null;
        Step prev = null;
        Step curr = null;

        for (DefaultMutableTreeNode node : path.getFullPath()) {
            String qName = node.getUserObject().toString();
            if ("".equals(qName.trim())) {
                continue;
            }

            int axis = qName.startsWith("@")?Step.AXIS_ATTRIBUTE:Step.AXIS_CHILD;
            curr = new Step(axis, qName);
            
            if (prev == null) {
                root = curr;
            } else {
                prev.setNext(curr);
                curr.setPrev(prev);
            }
            prev = curr;
        }
        
        this.rootStep = root;
        this.dataSource = path.getDataSource().getName();
        this.contextDb = path.getContextDb();
        this.lastStep = constructLastStep();
    }
    
    private Step constructLastStep() {
        Step last = getRootStep();
        while (last != null && last.getNext() != null) {
            last = last.getNext();
        }
        return last;
    }
    
    
    public Step getRootStep() {
        return rootStep;
    }

    public Step getLastStep(){
        return lastStep;
    }
    
    public String getDataSource() {
        return dataSource;
    }

    public DatabaseInfo getContextDb() {
        return contextDb;
    }
    
    /**
     * Gets the XPath representation of the text
     * @return
     */
    public String getText(){
        StringBuilder sb = new StringBuilder();
        
        Step next = getRootStep();
        while(next != getLastStep()){
            sb.append(next.getText());
            next = next.getNext();
        }
        sb.append(getLastStep().getText());
        return sb.toString();
    }
    
    /**
     * Gets the abbreviated XPath representation of the text
     * @return
     */
    public String getTextAbbreviation() {
        StringBuilder sb = new StringBuilder();
        
        Step next = getRootStep();
        while(next != getLastStep()){
            sb.append(next.getTextAbbreviation());
            next = next.getNext();
        }
        sb.append(getLastStep().getTextAbbreviation());
        return sb.toString();
    }

    /**
     * Gets the path expression in database format's representation
     * @return
     */
    public String getDbFormatText() {
        StringBuilder builder = new StringBuilder();
        Step nextStep = this.getRootStep();
        while (nextStep != null) {
            builder.append(".");
            builder.append(nextStep.getQName());
            builder.append("#");
            nextStep = nextStep.getNext();
        }
        return builder.toString();
    }
}
